﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace simulate_control.Helpers
{
    #region USING
    using System.Drawing;
    using DevComponents.DotNetBar.Controls;
    using DevComponents.DotNetBar;
    using simulate_control.Basic;
    using System.Globalization;
    #endregion

    public class Utils
    {
        public static Color ColorText = System.Drawing.Color.FromArgb(21, 66, 139);
        public static Font FontText = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(163)));

        /// <summary>
        /// Lua chon item ke tiep
        /// </summary>
        /// <param name="pnlMain"></param>
        /// <param name="dict"></param>
        /// <param name="Selected"></param>
        public static void RetrieveToNextItemPos(GroupPanel pnlMain, Dictionary<int, string> dict, ref int Selected)
        {
            if (Selected < dict.Count - 1)
            {
                Selected++;
            }
            else
            {
                Selected = 0;
            }
            Utils.ChangeSelectItem(pnlMain, dict, Selected);
        }

        /// <summary>
        /// Thay doi item duoc lua chon khi nhan phim up, down
        /// </summary>
        /// <param name="pnlMain"></param>
        /// <param name="dict"></param>
        /// <param name="selected"></param>
        /// <param name="keyEvent"></param>
        public static void ChangeSelectItemPos(GroupPanel pnlMain, Dictionary<int, string> dict, ref int selected, KEY keyEvent)
        {
            switch (keyEvent)
            {
                case KEY.UP:
                    if (selected > 0)
                    {
                        selected--;
                    }
                    break;
                case KEY.DOWN:
                    if (selected < dict.Count - 1)
                    {
                        selected++;
                    }
                    break;
            }
            Utils.ChangeSelectItem(pnlMain, dict, selected);
        }

        public static void ChangeSelectItem(GroupPanel pnlMain, Dictionary<int, string> dict, int selected)
        {
            foreach (var ctrl in pnlMain.Controls)
            {
                if (ctrl.GetType() == typeof(LabelX))
                {
                    var lbl = (LabelX)ctrl;
                    if (lbl.Name.StartsWith("lbl"))
                    {
                        var name = dict[selected];
                        if (lbl.Name == name)
                        {
                            lbl.BackColor = System.Drawing.Color.Crimson;
                            lbl.ForeColor = System.Drawing.Color.White;
                        }
                        else
                        {
                            lbl.BackColor = System.Drawing.Color.Transparent;
                            lbl.ForeColor = SystemColors.ControlText;
                        }
                    }
                }
                else if (ctrl.GetType() == typeof(HighlightLabel))
                {
                    var lbl = (HighlightLabel)ctrl;
                    if (lbl.Name.StartsWith("lbl"))
                    {
                        var name = dict[selected];
                        if (lbl.Name == name)
                        {
                            lbl.BackColor = System.Drawing.Color.Crimson;
                            lbl.LabelSelectedColor = Color.White;
                            lbl.HighlightPositions = new int[] { };
                        }
                        else
                        {
                            lbl.BackColor = System.Drawing.Color.Transparent;
                            lbl.LabelSelectedColor = Utils.ColorText;
                            lbl.HighlightPositions = new int[] { };
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Tu label name lay duoc label tuong ung
        /// </summary>
        /// <param name="pnlMain"></param>
        /// <param name="lblName"></param>
        /// <returns></returns>
        public static HighlightLabel GetSelectedHighlightLabel(GroupPanel pnlMain, string lblName)
        {
            foreach (var ctrl in pnlMain.Controls)
            {
                if (ctrl.GetType() == typeof(HighlightLabel))
                {
                    var lbl = (HighlightLabel)ctrl;
                    if (lbl.Name.StartsWith("lbl"))
                    {
                        if (lbl.Name == lblName)
                        {
                            return lbl;
                        }
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// Lay text cua item duoc lua chon
        /// </summary>
        /// <param name="pnlMain"></param>
        /// <param name="dict"></param>
        /// <param name="selected"></param>
        /// <returns></returns>
        public static string GetSelectedDataItem(GroupPanel pnlMain, Dictionary<int, string> dict, int selected)
        {
            foreach (var ctrl in pnlMain.Controls)
            {
                if (ctrl.GetType() == typeof(LabelX))
                {
                    var lbl = (LabelX)ctrl;
                    if (lbl.Name.StartsWith("lbl"))
                    {
                        var name = dict[selected];
                        if (lbl.Name == name)
                        {
                            return lbl.Text;
                        }
                    }
                }
            }
            return "";
        }

        public static string ReplaceCharWithChar(string org, int index, char charToUse)
        {
            char[] tmpBuffer = org.ToCharArray();
            tmpBuffer[index] = charToUse;
            return org = new string(tmpBuffer);
        }

        public static string CreateBlankString(int numBalanks)
        {
            switch (numBalanks)
            {
                case 1:
                    return " ";
                case 2:
                    return "  ";
                case 3: 
                    return "   ";
                case 4:
                    return "    ";
                case 5:
                    return "     ";
                case 6:
                    return "      ";
                case 7:
                    return "       ";
                case 8:
                    return "        ";
                case 9:
                    return "         ";
                case 10:
                    return "          ";
                case 11:
                    return "           ";
            }
            return " ";
        }

        public static void NextTimeVal(ref HighlightLabel lbl, int SelectedCharacterPos)
        {
            int val = 0;

            if (lbl.LabelText[SelectedCharacterPos] != ' ')
            {
                val = int.Parse(lbl.LabelText[SelectedCharacterPos].ToString(CultureInfo.CurrentCulture));

                switch (SelectedCharacterPos)
                {
                    case 0:
                        if (val < 2)
                        {
                            val++;
                        }
                        break;
                    case 1:
                        if (int.Parse(lbl.LabelText[SelectedCharacterPos - 1].ToString(CultureInfo.CurrentCulture)) < 2)
                        {
                            if (val < 9)
                            {
                                val++;
                            }
                        }
                        else
                        {
                            if (val < 3)
                            {
                                val++;
                            }
                        }
                        break;
                    case 3:
                        if (val < 5)
                        {
                            val++;
                        }
                        break;
                    case 4:
                        if (val < 9)
                        {
                            val++;
                        }
                        break;
                }
            }

            lbl.LabelText = Utils.ReplaceCharWithChar(lbl.LabelText, SelectedCharacterPos, char.Parse(val.ToString()));
            lbl.Refresh();
        }

        public static void PreviousTimeVal(ref HighlightLabel lbl, int SelectedCharacterPos)
        {
            int val = 0;

            if (lbl.LabelText[SelectedCharacterPos] != ' ')
            {
                val = int.Parse(lbl.LabelText[SelectedCharacterPos].ToString(CultureInfo.CurrentCulture));

                switch (SelectedCharacterPos)
                {
                    case 0:
                        if (val > 0)
                        {
                            val--;
                        }
                        break;
                    case 1:
                        if (val > 0)
                        {
                            val--;
                        }
                        break;
                    case 2:
                        if (val > 0)
                        {
                            val--;
                        }
                        break;
                    case 3:
                        if (val > 0)
                        {
                            val--;
                        }
                        break;
                }
            }

            lbl.LabelText = Utils.ReplaceCharWithChar(lbl.LabelText, SelectedCharacterPos, char.Parse(val.ToString()));
            lbl.Refresh();
        }

        public static void MoveToPreviousDigit(ref HighlightLabel lbl, ref int SelectedCharacterPos, Dictionary<int, string> dict, int Selected)
        {
            if (SelectedCharacterPos > 0)
            {
                SelectedCharacterPos--;
                // Time diff
                if (dict[Selected] == lbl.Name)
                {
                    if (lbl.LabelText[SelectedCharacterPos].ToString(CultureInfo.CurrentCulture) == ":" ||
                        lbl.LabelText[SelectedCharacterPos].ToString(CultureInfo.CurrentCulture) == "-" ||
                        lbl.LabelText[SelectedCharacterPos].ToString(CultureInfo.CurrentCulture) == "." ||
                        lbl.LabelText[SelectedCharacterPos].ToString(CultureInfo.CurrentCulture) == "\u00b0")
                    {
                        SelectedCharacterPos--;
                    }
                }
                lbl.HighlightPositions = new int[] { SelectedCharacterPos };
                lbl.Refresh();
            }
        }

        public static void MoveToNextDigit(ref HighlightLabel lbl, ref int SelectedCharacterPos, Dictionary<int, string> dict, int Selected)
        {
            if (SelectedCharacterPos < lbl.LabelMaxLength - 1)
            {
                SelectedCharacterPos++;
                // Time diff
                if (dict[Selected] == lbl.Name)
                {
                    if (lbl.LabelText[SelectedCharacterPos].ToString(CultureInfo.CurrentCulture) == ":" ||
                        lbl.LabelText[SelectedCharacterPos].ToString(CultureInfo.CurrentCulture) == "-" ||
                        lbl.LabelText[SelectedCharacterPos].ToString(CultureInfo.CurrentCulture) == "." ||
                        lbl.LabelText[SelectedCharacterPos].ToString(CultureInfo.CurrentCulture) == "\u00b0")
                    {
                        SelectedCharacterPos++;
                    }
                }
                lbl.HighlightPositions = new int[] { SelectedCharacterPos };
                lbl.Refresh();
            }
        }

        public static void NextCharacter(ref HighlightLabel lbl, int SelectedCharacterPos)
        {
            char letter = lbl.LabelText[SelectedCharacterPos];
            switch (letter)
            {
                case ' ':
                    letter = 'A';
                    lbl.LabelText = Utils.ReplaceCharWithChar(lbl.LabelText, SelectedCharacterPos, (char)(((int)letter)));
                    break;
                case 'Z':
                    letter = '0';
                    lbl.LabelText = Utils.ReplaceCharWithChar(lbl.LabelText, SelectedCharacterPos, (char)(((int)letter)));
                    break;
                case '9':
                    letter = 'A';
                    lbl.LabelText = Utils.ReplaceCharWithChar(lbl.LabelText, SelectedCharacterPos, (char)(((int)letter)));
                    break;
                default:
                    lbl.LabelText = Utils.ReplaceCharWithChar(lbl.LabelText, SelectedCharacterPos, (char)(((int)letter + 1)));
                    break;
            }
            lbl.Refresh();
        }

        public static void PreviousCharacter(ref HighlightLabel lbl, int SelectedCharacterPos)
        {
            char letter = lbl.LabelText[SelectedCharacterPos];
            switch (letter)
            {
                case 'A':
                case ' ':
                    break;
                case '0':
                    letter = 'Z';
                    lbl.LabelText = Utils.ReplaceCharWithChar(lbl.LabelText, SelectedCharacterPos, (char)(((int)letter)));
                    break;
                default:
                    lbl.LabelText = Utils.ReplaceCharWithChar(lbl.LabelText, SelectedCharacterPos, (char)(((int)letter) - 1));
                    break;
            }
            lbl.Refresh();
        }

        public static void NextVal(ref HighlightLabel lbl, int SelectedCharacterPos)
        {
            char letter = lbl.LabelText[SelectedCharacterPos];
            switch (letter)
            {
                case ' ':
                    letter = '0';
                    lbl.LabelText = Utils.ReplaceCharWithChar(lbl.LabelText, SelectedCharacterPos, (char)(((int)letter)));
                    break;
                case '9':
                    letter = '.';
                    lbl.LabelText = Utils.ReplaceCharWithChar(lbl.LabelText, SelectedCharacterPos, (char)(((int)letter)));
                    break;
                case '.':
                    letter = '0';
                    lbl.LabelText = Utils.ReplaceCharWithChar(lbl.LabelText, SelectedCharacterPos, (char)(((int)letter)));
                    break;
                default:
                    lbl.LabelText = Utils.ReplaceCharWithChar(lbl.LabelText, SelectedCharacterPos, (char)(((int)letter + 1)));
                    break;
            }
            lbl.Refresh();
        }

        public static void PreviousVal(ref HighlightLabel lbl, int SelectedCharacterPos)
        {
            char letter = lbl.LabelText[SelectedCharacterPos];
            switch (letter)
            {
                case ' ':
                    letter = '.';
                    lbl.LabelText = Utils.ReplaceCharWithChar(lbl.LabelText, SelectedCharacterPos, (char)(((int)letter)));
                    break;
                case '0':
                    letter = '.';
                    lbl.LabelText = Utils.ReplaceCharWithChar(lbl.LabelText, SelectedCharacterPos, (char)(((int)letter)));
                    break;
                case '.':
                    letter = '9';
                    lbl.LabelText = Utils.ReplaceCharWithChar(lbl.LabelText, SelectedCharacterPos, (char)(((int)letter)));
                    break;
                default:
                    lbl.LabelText = Utils.ReplaceCharWithChar(lbl.LabelText, SelectedCharacterPos, (char)(((int)letter - 1)));
                    break;
            }
            lbl.Refresh();
        }

        public static DISPMODE GetNextDispMode(DISPMODE curMode)
        {
            DISPMODE nextMode = DISPMODE.NONE;
            switch (curMode)
            {
                case DISPMODE.NONE:
                    nextMode = DISPMODE.CURSOR_TURN_ON;
                    break;
                case DISPMODE.CURSOR_TURN_ON:
                    break;
            }
            return nextMode;
        }

        /// <summary>
        /// Highlight ki tu tai vi tri index
        /// </summary>
        /// <param name="lbl"></param>
        /// <param name="SelectedCharacterPos"></param>
        public static void UpdateHighlightLabels(ref HighlightLabel lbl, int SelectedCharacterPos)
        {
            lbl.HighlightPositions = new int[] { SelectedCharacterPos };
            lbl.HighlightColor = Utils.ColorText;
            lbl.Refresh();
        }

        public static bool ItemExistInArray(string[] arr, string name)
        {
            foreach (var str in arr)
            {
                if (str.Trim().ToUpper() == name.Trim().ToUpper())
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Thay doi gia tri tai vi tri index
        /// </summary>
        /// <param name="lbl"></param>
        /// <param name="dict"></param>
        /// <param name="Selected"></param>
        /// <param name="SelectedCharacterPos"></param>
        /// <param name="keyEvent"></param>
        /// <param name="arrTimeVal"></param>
        /// <param name="arrChar"></param>
        /// <param name="arrVal"></param>
        public static void ChangeCharacterPos(ref HighlightLabel lbl, Dictionary<int, string> dict, int Selected, ref int SelectedCharacterPos, KEY keyEvent, string[] arrTimeVal, string[] arrChar, string[] arrVal)
        {
            switch (keyEvent)
            {
                case KEY.UP:// Den gia tri ke tiep
                    if (ItemExistInArray(arrTimeVal, lbl.Name))
                    {
                        Utils.NextTimeVal(ref lbl, SelectedCharacterPos);
                    }
                    else if (ItemExistInArray(arrChar, lbl.Name))
                    {
                        Utils.NextCharacter(ref lbl, SelectedCharacterPos);
                    }
                    else if (ItemExistInArray(arrVal, lbl.Name))
                    {
                        Utils.NextVal(ref lbl, SelectedCharacterPos);
                    }
                    break;
                case KEY.DOWN:// Tro ve gia tri truoc
                    if (ItemExistInArray(arrTimeVal, lbl.Name))
                    {
                        Utils.PreviousTimeVal(ref lbl, SelectedCharacterPos);
                    }
                    else if (ItemExistInArray(arrChar, lbl.Name))
                    {
                        Utils.PreviousCharacter(ref lbl, SelectedCharacterPos);
                    }
                    else if (ItemExistInArray(arrVal, lbl.Name))
                    {
                        Utils.PreviousVal(ref lbl, SelectedCharacterPos);
                    }
                    break;
                case KEY.LEFT:
                    // Den ki tu tiep theo
                    Utils.MoveToPreviousDigit(ref lbl, ref SelectedCharacterPos, dict, Selected);
                    break;
                case KEY.RIGHT:
                    // Tro ve ki tu truoc
                    Utils.MoveToNextDigit(ref lbl, ref SelectedCharacterPos, dict, Selected);
                    break;
            }
        }

        public static void InitDict(ref Dictionary<int, string> dict, string[] lstHighlightLabelName)
        {
            for (int i = 0; i < lstHighlightLabelName.Length; i++)
            {
                dict.Add(i, lstHighlightLabelName[i]);
            }
        }

        /// <summary>
        /// Khoi tao danh sach cac highlight labels
        /// </summary>
        /// <param name="pnlMain"></param>
        /// <param name="lstHighlightLabelName"></param>
        public static void InitHighlightLabels(ref GroupPanel pnlMain, string[] lstHighlightLabelName)
        {
            int firsty = 60;
            int OFFY = 29;
            for (int i = 0; i < lstHighlightLabelName.Length; i++)
            {
                var lbl = new HighlightLabel();
                InitLabel(ref pnlMain, lbl, lstHighlightLabelName[i], Utils.CreateBlankString(10), firsty);
                firsty += OFFY;
            }
        }

        /// <summary>
        /// Khoi tao 1 highlight label
        /// </summary>
        /// <param name="pnlMain"></param>
        /// <param name="lbl"></param>
        /// <param name="name"></param>
        /// <param name="text"></param>
        /// <param name="y"></param>
        public static void InitLabel(ref GroupPanel pnlMain, HighlightLabel lbl, string name, string text, int y)
        {
            lbl.LabelText = text;
            lbl.BackColor = System.Drawing.Color.Transparent;
            lbl.Name = name;
            lbl.TabIndex = 1;
            lbl.TextAlignment = System.Drawing.StringAlignment.Center;
            lbl.HighlightPositions = new int[] { };
            lbl.LabelMaxLength = text.Length;
            lbl.Size = new System.Drawing.Size(100, 23);
            lbl.Location = new System.Drawing.Point(187, y);

            pnlMain.Controls.Add(lbl);
        } 
    }
}
